import Vue from 'vue'
import VueRouter from 'vue-router'
import Dashboard from '@views/Dashboard'
import App from '@views/App'
import Loading from "@views/Loading"
import { isLoad } from '@utils/index'
import { Message } from 'element-ui';
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/app'
  },
  {
    path: '/app/loading',
    meta: {
      auth: true
    },
    component: Loading,

  },
  {
    path: '/app',
    name: '首页',
    redirect: '/app/dashboard',
    component: App,
    children: [
      {
        path: '/app/dashboard',
        name: '仪表板',
        mate: {
          icon: "el-icon-s-data",
          isNav: true
        },
        component: Dashboard
      },
      {
        path: '/app/home',
        name: '列表页',
        mate: {
          icon: "el-icon-document-remove",
          isNav: true
        },
        component: () => import('@views/Home')
      },
      {
        path: '/app/user',
        name: '管理员页',
        mate: {
          icon: "el-icon-s-custom",
          isNav: true
        },
        component: () => import('@views/User')
      },
      {
        path: '/app/setting',
        name: '设置页',
        mate: {
          icon: "el-icon-setting",
          isNav: true
        },
        component: () => import('@views/Setting')
      },
      {
        path: '/app/artEdit/:id',
        name: '编辑页',
        mate: {
          isNav: false
        },
        component: () => import('@views/ArtEdit')
      },
      {
        path: '/app/artAdd',
        name: '增加页',
        mate: {
          isNav: false
        },
        component: () => import('@views/ArtAdd')
      },
    ]
  },

]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {

  if (to.meta.auth) {
    next()
  } else {
    if (to.path === '/app/loading') {
      next()
    } else {
      if (!isLoad()) {
        next('/app/loading')

      } else {
        next()
      }
    }
  }

})

const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router